package com.yungu.swift.order.helper;

import com.yungu.swift.order.model.dto.OrderFareDto;

import java.math.BigDecimal;

/**
 * 描述：出租车费用详情
 *
 * @author Zerrion
 * @Time 2018/2/8
 */
public class CostDetailsHelper {

    /**
     * 费用详情
     */
    private OrderFareDto costDetail;
    /**
     * 总费用
     */
    private double totalFee = 0;

    public CostDetailsHelper(OrderFareDto orderFareDto) {
        this.costDetail = orderFareDto;
    }

    /**
     * 添加调度费
     *
     * @return
     */
    public CostDetailsHelper addTip() {
        isInit();
        double tip = costDetail.getTip() == null ? 0 : costDetail.getTip().doubleValue();
        totalFee += tip;
        return this;
    }

    /**
     * 添加等待费
     *
     * @return
     */
    public CostDetailsHelper addWaitFare() {
        isInit();
        double waitFare = costDetail.getWaitFare() == null ? 0 : costDetail.getWaitFare().doubleValue();
        totalFee += waitFare;
        return this;
    }

    /**
     * 添加服务费
     *
     * @return
     */
    public CostDetailsHelper addServiceFare() {
        isInit();
        double serviceFare = costDetail.getServiceFare() == null ? 0 : costDetail.getServiceFare().doubleValue();
        totalFee += serviceFare;
        return this;
    }

    /**
     * 添加打车费
     *
     * @return
     */
    public CostDetailsHelper addStartFare() {
        isInit();
        double startFare = costDetail.getStartFare() == null ? 0 : costDetail.getStartFare().doubleValue();
        totalFee += startFare;
        return this;
    }


    /**
     * 添加取消费用
     *
     * @return
     */
    public CostDetailsHelper addCancelFare() {
        isInit();
        double cancelFare = costDetail.getCancelFare() == null ? 0 : costDetail.getCancelFare().doubleValue();
        totalFee += cancelFare;
        return this;
    }


    /**
     * 获取明细
     *
     * @return OrderFareDto
     */
    public OrderFareDto getCostDetail() {
        return costDetail;
    }

    /**
     * 获取总费用
     *
     * @return double
     */
    public double getTotalFee() {
        if (totalFee != 0d) {
            return getDoubleValue(totalFee, 2);
        }
        return totalFee;
    }

    /**
     * 四舍五入
     *
     * @param value double
     * @param digit int
     * @return double
     */
    public static double getDoubleValue(double value, int digit) {
        if (digit < 0) {
            return 0;
        }
        BigDecimal bd = BigDecimal.valueOf(value).setScale(digit, BigDecimal.ROUND_HALF_UP);
        return bd.doubleValue();
    }


    /**
     * 判断参数状态
     *
     * @throws IllegalStateException
     */
    public void isInit() throws IllegalStateException {
        if (costDetail == null) {
            throw new IllegalStateException("未获取费用详情");
        }
    }
}
